import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author LW
 * @2020/8/16 15:33
 */
//练习Thread，实现多线程同步下载图片
public class TestThread2 extends Thread{

    private String url;
    private String name;

    //构造器
    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    //下载图片线程的执行体
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为"+name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://ae01.alicdn.com/kf/U22b1e36cdf6148dc919d86103e93e86cN.jpg","釜山行2");
        TestThread2 t2 = new TestThread2("https://ae01.alicdn.com/kf/U5cbb63a5c00c4797b6dab9f1d10e43d96.jpg","孙悟空大战盘丝洞");
        TestThread2 t3 = new TestThread2("https://ae01.alicdn.com/kf/Ue467c644d07b44ba902efc8acb7136db0.jpg","西行纪");
        t1.start();
        t2.start();
        t3.start();
        //理想是顺序
        //但每次不尽相同
    }
}


//下载器
class WebDownloader{
    //下载方法
    public void downloader(String url,String name){
        //文件工具
        try {
            //文件下载
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            System.out.println("IO异常,downloader方法出现问题");
            e.printStackTrace();
        }
    }
}